package cn.acmoj.oj.controller;

import cn.acmoj.oj.bean.Contest;
import cn.acmoj.oj.bean.Problem;
import cn.acmoj.oj.service.ContestService;
import cn.acmoj.oj.service.ProblemService;
import cn.acmoj.oj.service.ProblemSetService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * cn.acmoj.oj.controller
 * Springer
 * 2017-12-23
 */
@Controller
@RequestMapping(value = "contest")
public class ContestProblemController {

    @Resource
    private ContestService contestService;
    @Resource
    private ProblemSetService problemSetService;

    @RequestMapping(value = "problem", method = RequestMethod.GET)
    public String problem(@RequestParam(required = false, defaultValue = "-1") Integer cid,
                          @RequestParam(required = false, defaultValue = "") String pid,
                          HttpSession session,
                          HttpServletRequest request) {
        if (cid != null && cid == -1) {
            cid = (Integer) session.getAttribute("cid");
        }
        if (cid == null) {
            return "contest/error";
        }
        int problem_id = pid.charAt(0) - 65;
        Contest contest = contestService.getOnce(cid);
        Problem problem = problemSetService.getContestProblem(cid, problem_id);
        problem.setTime_limit(problem.getTime_limit() * 1000);

        request.setAttribute("contest", contest);
        request.setAttribute("problem", problem);
        return "contest/problem";
    }
}
